home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / gs262 / gsfont.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-29  |  9.3 KB  |  310 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsfont.c */
  20. /* Font operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "gserrors.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"
  26. #include "gzstate.h"            /* must precede gxdevice */
  27. #include "gxdevice.h"            /* must precede gxfont */
  28. #include "gschar.h"
  29. #include "gxfont.h"
  30. #include "gxfdir.h"
  31.  
  32. /* Imported procedures */
  33. void    gs_purge_font_from_char_caches(P2(gs_font_dir *, const gs_font *));
  34. #ifdef AMIGA
  35. extern void gx_char_cache_init(register gs_font_dir *);
  36. #endif
  37.  
  38. /* Size of cache structures */
  39. extern const uint cached_char_sizeof;
  40. extern const uint cached_fm_pair_sizeof;
  41.  
  42. /* Define the sizes of the various aspects of the font/character cache. */
  43. /*** Big memory machines ***/
  44. #define smax_LARGE 50        /* smax - # of scaled fonts */
  45. #define bmax_LARGE 500000    /* bmax - space for cached chars */
  46. #define mmax_LARGE 200        /* mmax - # of cached font/matrix pairs */
  47. #define cmax_LARGE 5000        /* cmax - # of cached chars */
  48. #define blimit_LARGE 2500    /* blimit/upper - max size of a single cached char */
  49. /*** Small memory machines ***/
  50. #define smax_SMALL 20        /* smax - # of scaled fonts */
  51. #define bmax_SMALL 25000    /* bmax - space for cached chars */
  52. #define mmax_SMALL 40        /* mmax - # of cached font/matrix pairs */
  53. #define cmax_SMALL 500        /* cmax - # of cached chars */
  54. #define blimit_SMALL 100    /* blimit/upper - max size of a single cached char */
  55.  
  56. /* Allocate a font directory */
  57. gs_font_dir *
  58. gs_font_dir_alloc(const gs_memory_procs *mprocs)
  59. {    /* Try allocating a very large cache. */
  60.     /* If this fails, allocate a small one. */
  61. #if !arch_ints_are_short
  62.     gs_font_dir *pdir;
  63.     pdir = gs_font_dir_alloc_limits(mprocs,
  64.                     smax_LARGE, bmax_LARGE, mmax_LARGE,
  65.                     cmax_LARGE, blimit_LARGE);
  66.     if ( pdir != 0 ) return pdir;
  67. #endif
  68.     return gs_font_dir_alloc_limits(mprocs,
  69.                     smax_SMALL, bmax_SMALL, mmax_SMALL,
  70.                     cmax_SMALL, blimit_SMALL);
  71. }
  72. gs_font_dir *
  73. gs_font_dir_alloc_limits(const gs_memory_procs *mprocs,
  74.   uint smax, uint bmax, uint mmax, uint cmax, uint upper)
  75. {    register gs_font_dir *pdir = (gs_font_dir *)(*mprocs->alloc)(1, sizeof(gs_font_dir), "font_dir_alloc(dir)");
  76.     uint chsize = (cmax / 5) | 31;        /* a guess */
  77.     cached_fm_pair *mdata;
  78.     cached_char **chars;
  79.     if ( pdir == 0 ) return 0;
  80.     /* Round up chsize to a power of 2. */
  81.     while ( chsize & (chsize + 1) ) chsize |= chsize >> 1;
  82.     chsize++;
  83.     mdata = (cached_fm_pair *)(*mprocs->alloc)(mmax, cached_fm_pair_sizeof, "font_dir_alloc(mdata)");
  84.     chars = (cached_char **)(*mprocs->alloc)(chsize, sizeof(cached_char *), "font_dir_alloc(chars)");
  85.     if ( mdata == 0 || chars == 0 )
  86.        {    if ( chars != 0 ) (*mprocs->free)((char *)chars, chsize, sizeof(cached_char *), "font_dir_alloc(chars)");
  87.         if ( mdata != 0 ) (*mprocs->free)((char *)mdata, mmax, cached_fm_pair_sizeof, "font_dir_alloc(mdata)");
  88.         (*mprocs->free)((char *)pdir, 1, sizeof(gs_font_dir), "font_dir_alloc(dir)");
  89.         return 0;
  90.        }
  91.     memset((char *)pdir, 0, sizeof(gs_font_dir));    /* easiest to clear everything first */
  92.     pdir->mprocs = mprocs;
  93.     pdir->smax = smax;
  94.     pdir->fmcache.mmax = mmax;
  95.     pdir->fmcache.mdata = mdata;
  96.     pdir->ccache.mprocs = mprocs;
  97.     pdir->ccache.bmax = bmax;
  98.     pdir->ccache.cmax = cmax;
  99.     pdir->ccache.lower = upper / 10;
  100.     pdir->ccache.upper = upper;
  101.     pdir->ccache.chars = chars;
  102.     pdir->ccache.chars_mask = chsize - 1;
  103.     gx_char_cache_init(pdir);
  104.     return pdir;
  105. }
  106.  
  107. /* Macro for linking an element at the head of a chain */
  108. #define link_first(first, elt)\
  109.   if ( (elt->next = first) != NULL ) first->prev = elt;\
  110.   elt->prev = 0;\
  111.   first = elt
  112.  
  113. /* definefont */
  114. /* Use this only for original (unscaled) fonts! */
  115. int
  116. gs_definefont(gs_font_dir *pdir, gs_font *pfont)
  117. {    link_first(pdir->orig_fonts, pfont);
  118.     pfont->dir = pdir;
  119.     pfont->base = pfont;
  120.     return 0;
  121. }
  122.  
  123. /* scalefont */
  124. int
  125. gs_scalefont(gs_font_dir *pdir, const gs_font *pfont, floatp scale,
  126.   gs_font **ppfont, gs_font **pdfont)
  127. {    gs_matrix mat;
  128.     gs_make_scaling(scale, scale, &mat);
  129.     return gs_makefont(pdir, pfont, &mat, ppfont, pdfont);
  130. }
  131.  
  132. /* makefont */
  133. int
  134. gs_makefont(gs_font_dir *pdir, const gs_font *pfont, const gs_matrix *pmat,
  135.   gs_font **ppfont, gs_font **pdfont)
  136. {    int code;
  137.     gs_font *prev = 0;
  138.     gs_font *pf_out = pdir->scaled_fonts;
  139.     gs_matrix newmat;
  140.     *pdfont = 0;
  141.     gs_make_identity(&newmat);    /* fill in tags */
  142.     if ( (code = gs_matrix_multiply(&pfont->FontMatrix, pmat, &newmat)) < 0 )
  143.       return code;
  144.     /* Check for the font already being in the scaled font cache. */
  145.     /* Only attempt to share fonts if the current font has */
  146.     /* a valid UniqueID or XUID. */
  147. #ifdef DEBUG
  148. if ( gs_debug['m'] )
  149.    {    if ( pfont->data.base.UID.size == 0 )    /* UniqueID */
  150.       dprintf1("[m]UniqueID=%ld", pfont->data.base.UID.u.id);
  151.     else
  152.       dprintf1("[m]XUID(%d)", pfont->data.base.UID.size);
  153.     dprintf7(", FontType=%d,\n[m]  new FontMatrix=[%g %g %g %g %g %g]\n",
  154.       pfont->FontType,
  155.       pmat->xx, pmat->xy, pmat->yx, pmat->yy,
  156.       pmat->tx, pmat->ty);
  157.    }
  158. #endif
  159.     if ( uid_is_valid(&pfont->data.base.UID) )
  160.       for ( ; pf_out != 0; prev = pf_out, pf_out = pf_out->next )
  161.         if ( uid_equal(&pf_out->data.base.UID, &pfont->data.base.UID) &&
  162.          pf_out->base == pfont->base &&
  163.          pf_out->FontType == pfont->FontType &&
  164.          pf_out->FontMatrix.xx == newmat.xx &&
  165.          pf_out->FontMatrix.xy == newmat.xy &&
  166.          pf_out->FontMatrix.yx == newmat.yx &&
  167.          pf_out->FontMatrix.yy == newmat.yy &&
  168.          pf_out->FontMatrix.tx == newmat.tx &&
  169.          pf_out->FontMatrix.ty == newmat.ty
  170.            )
  171.         {    *ppfont = pf_out;
  172.             if_debug1('m', "[m]found font=%lx\n", (ulong)pf_out);
  173.             return 0;
  174.         }
  175.     pf_out = (gs_font *)(*pdir->mprocs->alloc)(1, sizeof(gs_font), "gs_makefont");
  176.     if ( !pf_out ) return_error(gs_error_VMerror);
  177.     *pf_out = *pfont;
  178.     pf_out->FontMatrix = newmat;
  179.     pf_out->client_data = 0;
  180.     if ( uid_is_valid(&pfont->data.base.UID) )
  181.     {    if ( pdir->ssize == pdir->smax )
  182.         {    /* Must discard a cached scaled font. */
  183.             /* prev points to the last (oldest) font. */
  184.             if_debug1('m', "[m]discarding font %lx\n",
  185.                   (ulong)prev);
  186.             *pdfont = prev;
  187.             prev->prev->next = 0;
  188.         }
  189.         else
  190.             pdir->ssize++;
  191.         link_first(pdir->scaled_fonts, pf_out);
  192.     }
  193.     pf_out->dir = pdir;
  194.     pf_out->base = pfont->base;
  195.     *ppfont = pf_out;
  196.     if_debug1('m', "[m]new font=%lx\n", (ulong)pf_out);
  197.     return 1;
  198. }
  199.  
  200. /* setfont */
  201. int
  202. gs_setfont(gs_state *pgs, gs_font *pfont)
  203. {    pgs->font = pfont;
  204.     pgs->char_tm_valid = 0;
  205.     return 0;
  206. }
  207.  
  208. /* currentfont */
  209. gs_font *
  210. gs_currentfont(const gs_state *pgs)
  211. {    return pgs->font;
  212. }
  213.  
  214. /* cachestatus */
  215. void
  216. gs_cachestatus(register const gs_font_dir *pdir, register uint pstat[7])
  217. {    pstat[0] = pdir->ccache.bsize;
  218.     pstat[1] = pdir->ccache.bmax;
  219.     pstat[2] = pdir->fmcache.msize;
  220.     pstat[3] = pdir->fmcache.mmax;
  221.     pstat[4] = pdir->ccache.csize;
  222.     pstat[5] = pdir->ccache.cmax;
  223.     pstat[6] = pdir->ccache.upper;
  224. }
  225.  
  226. /* setcachelimit */
  227. int
  228. gs_setcachelimit(gs_font_dir *pdir, uint size)
  229. {    pdir->ccache.upper = size;
  230.     return 0;
  231. }
  232.  
  233. /* setcacheparams */
  234. int
  235. gs_setcachelower(gs_font_dir *pdir, uint size)
  236. {    pdir->ccache.lower = size;
  237.     return 0;
  238. }
  239. int
  240. gs_setcacheupper(gs_font_dir *pdir, uint size)
  241. {    pdir->ccache.upper = size;
  242.     return 0;
  243. }
  244.  
  245. /* currentcacheparams */
  246. uint
  247. gs_currentcachelower(const gs_font_dir *pdir)
  248. {    return pdir->ccache.lower;
  249. }
  250. uint
  251. gs_currentcacheupper(const gs_font_dir *pdir)
  252. {    return pdir->ccache.upper;
  253. }
  254.  
  255. /* Dummy (ineffective) BuildChar/BuildGlyph procedure */
  256. int
  257. gs_no_build_char_proc(struct gs_show_enum_s *penum, gs_state *pgs,
  258.   gs_font *pfont, gs_char chr, gs_glyph glyph)
  259. {    return 1;            /* failure, but not error */
  260. }
  261.  
  262. /* Dummy character encoding procedure */
  263. gs_glyph
  264. gs_no_encode_char_proc(struct gs_show_enum_s *penum,
  265.   gs_font *pfont, gs_char *pchr)
  266. {    return gs_no_glyph;
  267. }
  268.  
  269. /* Purge a font from all font- and character-related tables. */
  270. /* This is only used by restore (and, someday, the GC). */
  271. void
  272. gs_purge_font(const gs_font *pfont)
  273. {    gs_font_dir *pdir = pfont->dir;
  274.     gs_font *pf;
  275.  
  276.     /* Remove the font from its list (orig_fonts or scaled_fonts). */
  277.     gs_font *prev = pfont->prev;
  278.     gs_font *next = pfont->next;
  279.     if ( next != 0 )
  280.         next->prev = prev;
  281.     if ( prev != 0 )
  282.         prev->next = next;
  283.     else if ( pdir->orig_fonts == pfont )
  284.         pdir->orig_fonts = next;
  285.     else if ( pdir->scaled_fonts == pfont )
  286.         pdir->scaled_fonts = next;
  287.     else
  288.         /* Shouldn't happen! */
  289.         ;
  290.     if ( pfont->base != pfont )
  291.     {    /* I.e., this is a scaled font. */
  292.         pdir->ssize--;
  293.     }
  294.  
  295.     /* Purge the font from the scaled font cache. */
  296.     for ( pf = pdir->scaled_fonts; pf != 0; )
  297.     {    if ( pf->base == pfont )
  298.         {    gs_purge_font(pf);
  299.             pf = pdir->scaled_fonts; /* start over */
  300.         }
  301.         else
  302.             pf = pf->next;
  303.     }
  304.  
  305.     /* Purge the font from the font/matrix pair cache, */
  306.     /* including all cached characters rendered with that font. */
  307.     gs_purge_font_from_char_caches(pdir, pfont);
  308.  
  309. }
  310.